﻿
using System.Net;
using RR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Account_Login : System.Web.UI.Page
{
    protected RegistRar regrar = new RegistRar();
    HttpCookie obj_cookie;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (HttpContext.Current.User.Identity.IsAuthenticated)
        {
            //var cheef = regrar.KONTRAGENT.Where(k => k.CHIEFFIRM == 1).Select(k=> k.CODE);
            var cheef = (from k in regrar.KONTRAGENT
                where k.CHIEFFIRM == 1
                select k).First();
            
            //if (Profile.UserInfo.ActiveKontragent != cheef.CODE)
            if (User.IsInRole("2"))
            {
                Response.Redirect("~/search.aspx");
            }
        }

        PrepareCombobox();
        if (!IsPostBack && !IsCallback)
        {
            //read cookie
            obj_cookie = Request.Cookies["reg_UserInfo"];
            if (obj_cookie != null)
            {
                object kcode = Convert.ToInt32(obj_cookie.Values["reg_ActiveKontragent"]);
                cbb_kontr.SelectedItem = cbb_kontr.Items.FindByValue(kcode);
                LoadWorker(Convert.ToInt32(cbb_kontr.Value));
                cbb_worker.SelectedItem = cbb_worker.Items.FindByValue(Convert.ToInt32(obj_cookie.Values["reg_ActiveWorker"]));
                ed_pass.Focus();
            }
        }
    }

    protected void PrepareCombobox()
    {
        cbb_kontr.ValueField = "CODE";
        cbb_kontr.AutoPostBack = true;
        //

        var query = from p in regrar.KONTRAGENT
                    select new { p.CODE, p.NAME };
        cbb_kontr.DataSource = query.ToArray();
        cbb_kontr.DataBind();
    }

    private void LoadWorker(int p)
    {
        cbb_worker.Value = null;
        var query = from w in regrar.WORKER
                    where w.KONTRAGENT == p && (w.PSW != null && w.PSW != String.Empty)
                    select w;
        cbb_worker.DataSource = query.ToArray();

        cbb_worker.ValueField = "CODE";
        cbb_worker.TextField = "FIO";
        cbb_worker.DataBind();

        cbb_worker.Focus();
    }

    protected void cbb_kontr_SelectedIndexChanged(object sender, EventArgs e)
    {
        LoadWorker(Convert.ToInt32(cbb_kontr.Value));
    }

    protected void btnLogin_Click(object sender, EventArgs e)
    {
        string pswd = GetPSWD(Convert.ToInt32(cbb_worker.Value));
        if (CheckPass(pswd))
        {
            FormsAuthentication.RedirectFromLoginPage(cbb_worker.Text, false);
            Saveprofile();
        }
        else
        {
            ed_pass.ErrorText = "Неверный пароль!";
            ed_pass.IsValid = false;
        }
    }

    private void Saveprofile()
    {
        int kontr = Convert.ToInt32(cbb_kontr.Value);
        int worker = Convert.ToInt32(cbb_worker.Value);

        Profile.Initialize(cbb_worker.Text, true);
        Profile.UserInfo.ActiveKontragent = kontr;
        Profile.UserInfo.ActiveWorker = worker;
        Profile.UserInfo.ActiveRole = GetRole(worker);

        Profile.UserInfo.KontrName = cbb_kontr.Text;
        Profile.UserInfo.UserFIO = cbb_worker.Text;


        if (!Roles.RoleExists(Convert.ToString(Profile.UserInfo.ActiveRole))) Roles.CreateRole(Convert.ToString(Profile.UserInfo.ActiveRole));

        if (!Roles.IsUserInRole(cbb_worker.Text, Convert.ToString(Profile.UserInfo.ActiveRole)))
            Roles.AddUserToRole(cbb_worker.Text, Convert.ToString(Profile.UserInfo.ActiveRole));

        HttpCookie theCookie = new HttpCookie("reg_UserInfo");
        theCookie["reg_ActiveKontragent"] = cbb_kontr.Value.ToString();
        theCookie["reg_ActiveWorker"] = cbb_worker.Value.ToString();
        Response.Cookies.Add(theCookie);
     
    }

    private string GetPSWD(int code)
    {
        var query = (from p in regrar.WORKER
                     where p.CODE == code
                     select p).First();
        return query.PSW;
    }

    private int GetRole(int worker)
    {
        var query = (from w in regrar.WORKER
                     where w.CODE == worker
                     select w).First();
        if (query != null)
        {
            return query.WORKERSTATE;
        }
        else { return -1; }
    }

    protected bool CheckPass(string psw)
    {
        return String.Compare(psw, ed_pass.Text) == 0;
    }
}